package drds.global_transaction.api.rpc.rpc_client.resource_manager;

import drds.common.Log;
import drds.global_transaction.api.in_bound_handler.TransactionMessageHandler;
import drds.global_transaction.api.message_codec.ResponseCode;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_commit_status_report.BranchTransactionCommitRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_commit_status_report.BranchTransactionCommitResponseMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_rollback_status_report.BranchTransactionRollbackRequestMessage;
import drds.global_transaction.api.message_codec.request_and_response.transaction.branch_transaction.branch_transaction_end_status_report.branch_transaction_rollback_status_report.BranchTransactionRollbackResponseMessage;
import drds.global_transaction.api.rpc.rpc_client.MessageSend;
import drds.global_transaction.common.exception.FrameworkErrorCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ResourceManagerClientMessageListener implements IResourceManagerClientMessageListener
{
	@Setter
	@Getter
	private TransactionMessageHandler transactionMessageHandler;

	public ResourceManagerClientMessageListener(TransactionMessageHandler transactionMessageHandler)
	{
		this.transactionMessageHandler = transactionMessageHandler;
	}

	private void handleBranchTransactionCommitStatusReport(String serverAddress, MessageSend messageSend, long messageId, BranchTransactionCommitRequestMessage branchTransactionCommitRequestMessage)
	{

		BranchTransactionCommitResponseMessage branchTransactionCommitResponseMessage = null;
		try
		{
			branchTransactionCommitResponseMessage = (BranchTransactionCommitResponseMessage) transactionMessageHandler.onRequest(null, branchTransactionCommitRequestMessage);
			messageSend.synchronousSendResponse(serverAddress, messageId, branchTransactionCommitResponseMessage);
		} catch (Exception e)
		{
			log.error(FrameworkErrorCode.NetOnMessage.errCode, e.getMessage(), e);
			branchTransactionCommitResponseMessage.setResponseCode(ResponseCode.failed);
			branchTransactionCommitResponseMessage.setMessage(e.getMessage());
			messageSend.synchronousSendResponse(serverAddress, messageId, branchTransactionCommitResponseMessage);
		}
	}

	private void handleBranchTransactionRollbackStatusReport(String serverAddress, MessageSend messageSend, long messageId, BranchTransactionRollbackRequestMessage branchTransactionRollbackRequestMessage)
	{
		BranchTransactionRollbackResponseMessage branchTransactionRollbackResponseMessage = null;
		branchTransactionRollbackResponseMessage = (BranchTransactionRollbackResponseMessage) transactionMessageHandler.onRequest(null, branchTransactionRollbackRequestMessage);
		try
		{
			messageSend.synchronousSendResponse(serverAddress, messageId, branchTransactionRollbackResponseMessage);
		} catch (Exception e)
		{
			Log.error(log, Thread.currentThread(), "handleBranchTransactionRollbackStatusReport", e);
		}
	}

	@Override
	public void onResourceManagerClientMessage(String serverAddress, long messageId, Object message, MessageSend messageSend)
	{
		if (message instanceof BranchTransactionCommitRequestMessage)
		{
			handleBranchTransactionCommitStatusReport(serverAddress, messageSend, messageId, (BranchTransactionCommitRequestMessage) message);
		} else if (message instanceof BranchTransactionRollbackRequestMessage)
		{
			handleBranchTransactionRollbackStatusReport(serverAddress, messageSend, messageId, (BranchTransactionRollbackRequestMessage) message);
		}
	}

}
